package com.atguigu.springcloud.gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 网关路由第二种方式【硬编码】： 代码中注入RouteLocator的Bean
 *
 * @author lenovo
 * @version 1.0
 * @date 2020/12/16 10:56
 */
@Configuration
public class GateWayConfig {

    /**
     * 配置了-个id为route-name的路由规则，
     * 当访问地址http://Localhost:9527/guonei时会自动转发到地址: http://news.baidu.com/guonei
     *
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator customRoute(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("path_route_1", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
        return routes.build();
    }


    @Bean
    public RouteLocator customRoute2(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("path_route_2", r -> r.path("/guoji").uri("http://news.baidu.com/guoji")).build();
        return routes.build();
    }


    @Bean
    public RouteLocator customRoute3(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
//        routes.route("path_route_3", r -> r.path("/mil").uri("http://news.baidu.com/mil")).build();
        routes.route(r -> r.path("/mil").uri("http://news.baidu.com/mil")).build();
        return routes.build();
    }
}
